package com.atguigu.tingshu.album.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.tingshu.album.mapper.*;
import com.atguigu.tingshu.album.service.BaseCategoryService;
import com.atguigu.tingshu.model.album.*;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.swagger.v3.core.util.Json;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@SuppressWarnings({"all"})
public class BaseCategoryServiceImpl extends ServiceImpl<BaseCategory1Mapper, BaseCategory1> implements BaseCategoryService {

	@Autowired
	private BaseCategory1Mapper baseCategory1Mapper;

	@Autowired
	private BaseCategory2Mapper baseCategory2Mapper;

	@Autowired
	private BaseCategory3Mapper baseCategory3Mapper;


	@Autowired
	private BaseCategoryViewMapper baseCategoryViewMapper;

	@Autowired
	private BaseAttributeMapper baseAttributeMapper;


	@Override
	public List<BaseAttribute> findAttribute(Long category1Id) {
		//	在mapper中写sql语句;
		return baseAttributeMapper.selectAttribute(category1Id);
	}

	@Override
	public List<BaseCategory3> findTopBaseCategory3(Long category1Id) {
		//	根据一级分类Id 查询二级分类数据Id 集合
		List<Long> category2IdList = baseCategory2Mapper.selectList(new LambdaQueryWrapper<BaseCategory2>().eq(BaseCategory2::getCategory1Id, category1Id)).stream().map(BaseCategory2::getId).collect(Collectors.toList());
		LambdaQueryWrapper<BaseCategory3> wrapper = new LambdaQueryWrapper<>();
		wrapper.in(BaseCategory3::getCategory2Id,category2IdList).eq(BaseCategory3::getIsTop,1).last(" limit 7 ");
		return baseCategory3Mapper.selectList(wrapper);
	}

	@Override
	public JSONObject getBaseCategoryList(Long category1Id) {
		//	创建一级分类对象
		JSONObject category1 = new JSONObject();
		List<BaseCategoryView> baseCategoryViewList = baseCategoryViewMapper.selectList(new LambdaQueryWrapper<BaseCategoryView>().eq(BaseCategoryView::getCategory1Id, category1Id));
		//	赋值：
		category1.put("categoryId",category1Id);
		//	一级分类名称;
		category1.put("categoryName",baseCategoryViewList.get(0).getCategory1Name());
		//	获取二级分类数据
		Map<Long, List<BaseCategoryView>> map = baseCategoryViewList.stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory2Id));
		Iterator<Map.Entry<Long, List<BaseCategoryView>>> iterator = map.entrySet().iterator();
		ArrayList<JSONObject> category2ChildList = new ArrayList<>();
		while (iterator.hasNext()){
			Map.Entry<Long, List<BaseCategoryView>> entry = iterator.next();
			Long category2Id = entry.getKey();
			List<BaseCategoryView> baseCategoryViewList1 = entry.getValue();
			//	创建一级分类对象
			JSONObject category2 = new JSONObject();
			category2.put("categoryId",category2Id);
			category2.put("categoryName",baseCategoryViewList1.get(0).getCategory2Name());
			//	将三级数据赋值给二级
			List<JSONObject> list = baseCategoryViewList1.stream().map(baseCategoryView -> {
				JSONObject category3 = new JSONObject();
				category3.put("categoryId", baseCategoryView.getCategory3Id());
				category3.put("categoryName", baseCategoryView.getCategory3Name());
				return category3;
			}).collect(Collectors.toList());
			//	将三级数据添加的二级对象中
			category2.put("categoryChild",list);
			//	将二级分类数据添加到集合中
			category2ChildList.add(category2);
		}
		//	将二级分类数据添加到一级对象中
		category1.put("categoryChild",category2ChildList);
		//	返回对象
		return category1;
	}

	@Override
	public BaseCategoryView getCategoryView(Long category3Id) {
		//	根据主键Id查询数据
		return this.baseCategoryViewMapper.selectById(category3Id);
	}

	@Override
	public List<JSONObject> getBaseCategoryList() {
		//	创建list 集合
		List<JSONObject> list = new ArrayList<>();
		//	先查询视图中的所有数据； select * from base_category_view
		List<BaseCategoryView> baseCategoryViewList = baseCategoryViewMapper.selectList(null);
		//	构建数据结构 -- 拿到之后如何去重！
		//	key = category1Id value=List<BaseCategoryView>
		Map<Long, List<BaseCategoryView>> map = baseCategoryViewList.stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory1Id));
		Iterator<Map.Entry<Long, List<BaseCategoryView>>> iterator = map.entrySet().iterator();
		while (iterator.hasNext()){
			//	获取数据
			Map.Entry<Long, List<BaseCategoryView>> entry = iterator.next();
			Long category1Id = entry.getKey();
			List<BaseCategoryView> baseCategoryViewList1 = entry.getValue();
			//	创建一个一级分类对象
			JSONObject category1 = new JSONObject();
			category1.put("categoryId",category1Id);
			category1.put("categoryName",baseCategoryViewList1.get(0).getCategory1Name());
			//	以二级分类Id 分组
			Map<Long, List<BaseCategoryView>> map1 = baseCategoryViewList1.stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory2Id));
			Iterator<Map.Entry<Long, List<BaseCategoryView>>> iterator1 = map1.entrySet().iterator();
			//	声明一个二级分类集合
			List<JSONObject> categoryChildList = new ArrayList<>();
			while (iterator1.hasNext()){
				//	创建一个二级分类对象
				JSONObject category2 = new JSONObject();
				Map.Entry<Long, List<BaseCategoryView>> entry1 = iterator1.next();
				//	获取key
				Long category2Id = entry1.getKey();
				//	获取value
				List<BaseCategoryView> baseCategoryViewList2 = entry1.getValue();
				category2.put("categoryId",category2Id);
				category2.put("categoryName",baseCategoryViewList2.get(0).getCategory2Name());
				//	获取三级分类数据
				List<JSONObject> category3ChildList = baseCategoryViewList2.stream().map(baseCategoryView -> {
					JSONObject category3 = new JSONObject();
					category3.put("categoryId", baseCategoryView.getCategory3Id());
					category3.put("categoryName", baseCategoryView.getCategory3Name());
					return category3;
				}).collect(Collectors.toList());

				//				List<JSONObject> category3ChildList1 = new ArrayList<>()
				//				for (BaseCategoryView baseCategoryView : baseCategoryViewList2) {
				//					JSONObject category3 = new JSONObject();
				//					category3.put("categoryId", baseCategoryView.getCategory3Id());
				//					category3.put("categoryName", baseCategoryView.getCategory3Name());
				//					category3ChildList1.add(category3);
				//				}
				//	添加三级分类集合
				category2.put("categoryChild",category3ChildList);
				categoryChildList.add(category2);
			}
			//	添加二级分类集合
			category1.put("categoryChild",categoryChildList);
			//	将一级分类对象添加到集合中
			list.add(category1);
		}
		//	返回数据
		return list;
	}
}
